//
//  NetHander.m
//  UI_封装网络请求
//
//  Created by 18_李明明 on 14-12-4.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NetHander.h"

@implementation NetHander
+(void)getDataWithUrl:(NSString *)str completion:(void (^)(NSData *))block
{
    NSString *urlstr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [[NSURL alloc] initWithString:urlstr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:30];
    
    request.HTTPMethod = @"GET";
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        // 处理数据
        
        // 确定地址
        NSString *docpath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *path = [NSString stringWithFormat:@"%@/%u", docpath, [str hash]];
//        NSLog(@"%@", path);
        if (data != nil) {
         // 当返回数据不是空的时候,调用block
            
//            BOOL result = [NSKeyedArchiver archiveRootObject:data toFile:path];
//            NSLog(@"%d", result);
            block(data);
        } else {
            // 没有数据
            NSData *pickData = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
            if (pickData != nil) {
                block(pickData);
            }
        }
    }];
    
    
    
}



@end
